using AhpilyServer;
using GameServer.Cache;
using GameServer.Cache.Fight;
using GameServer.Cache.Match;
using Protocol.Code;
using Protocol.Constant;
using Protocol.Dto;

namespace GameServer.Logic
{
    public class ChatHandler : IHeadler
    {
        private UserCache userCache = Caches.User;
        private MatchCache matchCache1 = Caches.Match1;
        private MatchCache2 matchCache2 = Caches.Match2;
        private FightCache fightCache = Caches.Fight;

        public void OnDisconnect(ClientPeer client)
        {
        }

        public void OnReceive(ClientPeer client, int SubCode, object value)
        {
            switch (SubCode)
            {
                case ChatCode.CREQ:
                    chatRequest(client, (int)value);
                    break;
                default:
                    break;
            }
        }

        private void chatRequest(ClientPeer client, int chatType)
        {
            if (userCache.IsOnline(client) == false)
                return;
            int userId = userCache.GetId(client);
            // 发什么?
            ChatDto dto = new ChatDto(userId, chatType);

            // 判断房间类型, 根据类型发送数据
            // 给谁?房间内的每一位玩家
            if (matchCache1.IsMatching(userId))
            {
                // 匹配房间
                MatchRoom mRoom = matchCache1.GetRoom(userId);
                mRoom.Brocast(OpCode.CHAT, ChatCode.SRES, dto);
            }
            else if (fightCache.IsFighting(userId))
            {
                // 这里检测战斗房间
                FightRoom room = fightCache.GetRoomByUid(userId);
                room.Brocast(OpCode.CHAT, ChatCode.SRES, dto);
            }
            //else if(matchRoomType == MatchRoomType.CATCH_TORTOISE_ROOM)
            //{
            //    // 根上面一样, 给房间内的玩家发消息
            //    // TODO
            //    if (matchCache2.IsMatching(userId))
            //    {
            //    }
            //    //else if(/*fightCache2.IsFighting(userId)*/)
            //    //{

            //    //}
            //}
           
        }
    }
}
